package com.hjy.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hjy.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000121649375";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQChssUnQPnICdKG8UVgHSKGs+3/vaXYLOaFL4VAkk9ZVxlApw+qpTUhrpXGeQuDtn/YbM6kjR+jcsIJNkFV1ErbOlfuIqSR/7djAvjZ9k2gW58i5+TfjhMvqxqZD78bKLJP3c1/8EhlNvFDzF4b4HeaARkamePuYmwzxaQMYI0Xc58NZfLXCU1VdQJXO4ruefCY+FonGsQeTu7onjaEl3ytNgivaxfSWmDTgz3xA5bMTMDxouoqwaBm55fKyCW4yfMuTmtZn19hzGl0v8Nyk/hCC9785H86wcodxgFSTR3d0rSJ51Ec2wz1oHeMZ1oteQdsQVb50/rYW2uFZJMiXv9zAgMBAAECggEBAJCRZtebrjb9IS7o0+AE7HOGt0kpY/eM3WVCgV6j8TGtP7SgKM+m+YPttZdHbdYK+u6jCXXxftIBrh3SVkPIlEzBMxCEBKNL/8gURi2Jmwm+IN9yeRyux9ScpCEpBPHhRttFMZArYm6r2rWFKLa/z+7vAGTaJgFiGd+H2w393OeTWVd99yWZbChhecNNa6rlMg97ZSmOIfFaCl+9BN/tpGWAesgOcTwzR+yr6H4WCx1ppTCpqwn9pMijx1aqZKWkwO8x13v0FZaKagix3HVVavOlmMxbyEe8XE5RQxETRnOaU5Hpb77lOQNRsCe7Rx7c1RFSxFRKtCa8CyC1bdcwnCECgYEA2xuBuhyPju607F5rN8xiSg46JI672kpLkBv2gRl6nLYqVGs1MrUpVxnw8xr2B3Hroi0pMsQUWuH4tcRZ1vGLU5h8YvpsjI+EtpYNtyyJdl/CZ7qSUlnjtXXRB7qowOTnhsy4JrN/l0Uj+o346nWxGtSwptBNyzh+coR/9hnzrykCgYEAvOytiSocachSTvnSfNAAwKg8iH+DkmfhGcc2xB7YLK41uXRcXzVpSxYHrMFZnq6aQYa6cxRTd32/epTjpVixXpmpbH5uLLdZaIezELUTmjt7qg3Gut/i00ZRwAG/S8oWesOnny+e1LJECt4+IyC8A7QheJ5E9EccBwHjANG5uTsCgYBqQW6p+MvNvwlST3+jcaSA0WF09Rs4gysd2dGzTc3eTaE9izgjPSOtL9Ffs8V4WtCwt0HxcpGEeW2JiI0g5cZxZdN8lIErsTXk7eP83sZS96FpeFsZQKpjB521J9pKoTd+ca0kZ4wKqecGzO7XlPesUVTRsNbpJJ59nrAHgxJf8QKBgQCaDKeIRAU9IHI1IbJHwdgLQGNzrf5cIEZFndKDYt9Et5zpJv+3Rrcf2cvSSyrHoHn52mEoOzfAJJJA4y0pA0RF7ihfZkxyO+M0wAn8IprkVYKF7IvSj4LGYf2BAiu5IgTof6ervsnVh7LB5exm+mc04em1D+EMCvvInvo6DclSRwKBgAtR1CYAhOO9/KnEtANlqlA+vm+JSJ4zOk/5+vAmCwLRHvu14sC1hDWipvgdKcDByJMFWd+Eaone/7SOYVYMZvNVFTCXRiPFlfdiunGcX+uvCIb7RmOUfEZihsNSdXyIxeFmTqAQeYjo0LsuGyJbyR9R7qensW5xLxDkAPBegao4";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsNo2xFyW9N5bs82AIRI5SCcfkUFz6MA+J9rkKP5Zj3xIr9nuyf2qPYt0hX1F6VUuzZ/eieE+QtWeYdgkxT12A+cbilIToIaFdniR5bNvF0VrexMSugdYxKM7K9fC8MtqgCIruw+fOwNOf42WOZEYG4GrtOUccypgUsoBz6HPNxsv760STx0+PRffPBlmpi6ISw8OEN8SOD/DZtxXBKEJuR2aYL2KqdQJdW35FLN1x9DLwPVdwdFc9Jn/Ka+Lvi98+Y4ADgvV3akbNFjyLfOMo4744inWysdcZLV3iNPEDC+Mx8ITe/x+h8ou1wwNKgoRitVptjfWOCQEFTymcc7AmQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://gulimall.com";

    //页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，跳转到成功页
    private String return_url = "http://order.gulimall.com/list.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
